﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;


namespace BT_web__nhom06
{
    public partial class Sua_CD : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string machidoan = Request.QueryString["id"];
                txt_machidoan.Text = machidoan;
                //string sqlselect1 = "select chidoan.machidoan,chidoan.tenchidoan,chidoan.masvbithuchidoan,chidoan.masvphobithuchidoan,doanvien.ho,doanvien.ten";
                string sqlselectChiDoan = "select * from ChiDoan where machidoan=@machidoan";
                string sqlselectMaSvBithuCuaChiDoan = "select doanvien.masinhvien,ho,ten from doanvien,chidoan where doanvien.masinhvien = chidoan.masvbithuchidoan and doanvien.machidoan = @machidoan";
                string sqlselectMaSvPhoBithuCuaChiDoan = "select doanvien.masinhvien,ho,ten from doanvien,chidoan where doanvien.masinhvien = chidoan.masvphobithuchidoan and doanvien.machidoan = @machidoan";

                if (machidoan == null)
                    Response.Redirect("QL_ChiDoan.aspx");
                DataTable tblchidoan = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlselectChiDoan, "@machidoan", machidoan);
                if (tblchidoan.Rows.Count > 0)
                {
                    DataRow dro = tblchidoan.Rows[0];
                    txttenchidoan.Text = (string)dro["tenchidoan"];
                    txtdacdiemchidoan.Text = (string)dro["dacdiem"];
                }
                DataTable tbldoanvien = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlselectMaSvBithuCuaChiDoan, "@machidoan", machidoan);
                if (tbldoanvien.Rows.Count > 0)
                {
                   DataRow dr = tbldoanvien.Rows[0];
                   txtbithuchidoan.Text = (string)dr["masinhvien"];
                }
                DataTable tblphobithuchidoan = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlselectMaSvPhoBithuCuaChiDoan, "@machidoan", machidoan);
                if(tblphobithuchidoan.Rows.Count>0)
                {
                    DataRow drr = tblphobithuchidoan.Rows[0];
                    txtphobithuchidoan.Text = (string)drr["masinhvien"];
                }
               
            }
        }
        
        
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("QL_ChiDoan.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string masvbithuchidoan = txtbithuchidoan.Text;
            string masvphobithuchidoan = "";
            if (txtphobithuchidoan.Text != "")
                masvphobithuchidoan = txtphobithuchidoan.Text;
            string daccdiem = txtdacdiemchidoan.Text;
            string machidoan = txt_machidoan.Text;
            int ketqua = 0;
            string lenhsql = "update chidoan set [masvbithuchidoan] = @masvbithuchidoan, [masvphobithuchidoan]=@masvphobithuchidoan,[dacdiem]=@dacdiem where [machidoan] = @machidoan";
            ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(lenhsql, new string[] { "@masvbithuchidoan", "@masvphobithuchidoan", "@dacdiem","@machidoan" }, new object[] { masvbithuchidoan,masvphobithuchidoan,daccdiem,machidoan });
            if (ketqua == 1)
                Label1.Text = "cap nhat thanh cong!";
            else
                Label1.Text = "cap nhat khong thanh cong!";
        }

        protected void dlsbmasv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string masinhvien = txtbithuchidoan.Text;
            string sqlSelect = "SELECT * FROM [doanvien],[chidoan] WHERE [doanvien].[masinhvien]=@masinhvien and [doanvien].[masinhvien]=[chidoan].[masvbithuchidoan] ";
            DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@masinhvien", masinhvien);
            if (tblKetQua.Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
                CustomValidator1.ErrorMessage = "Mã sinh viên không tồn tại";
            }
                
        }

        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (txtphobithuchidoan.Text != "")
            {
                string masinhvien = txtbithuchidoan.Text;
                string sqlSelect = "SELECT * FROM [doanvien],[chidoan] WHERE [doanvien].[masinhvien]=@masinhvien and [doanvien].[masinhvien]=[chidoan].[masvphobithuchidoan] ";
                DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@masinhvien", masinhvien);
                if (tblKetQua.Rows.Count > 0)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                    CustomValidator3.ErrorMessage = "Mã sinh viên không tồn tại";
                }

            }
        }
        
    }
}